<template>
	<view>
		<u-tabs :list="list1" @click="click" :activeStyle="activeStyle" lineColor="#17B99D"
			:scrollable="false"></u-tabs>
		<view>
			<navigator :url="'/pages/suggest/detail?id='+item.id" hover-class="none" class="list" v-for="(item,index) in list" :key="index">
				<view class="title">反馈内容：{{item.content}}</view>
				<view class="flex between">
					<view>提交时间：{{item.create_time}}</view>
					<view>回复状态：{{item.status==1?'未答复':'已答复'}}</view>
				</view>
			</navigator>
			<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		</view>
	</view>
</template>

<script>
	import {
		feedbackMyList
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				totalPage: 1,
				page: 0,
				offset: 10,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				list1: [{
					name: '待答复',
				}, {
					name: '已答复',
				}, ],
				index: 0,
				activeStyle: {
					color: '#17B99D'
				},
				status: 1,
				ws: null,
				path: '/pages/suggest/list'
			};
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		onLoad() {
			this.getlist()
		},
		onShow() {
			this.connectWebSocket()
		},
		onHide() {
			// 页面隐藏/切入后台时触发
			let that = this
			that.sendData({
				type: 'page_leave',
				path: that.path,
				params: ""
			});
		},
		onUnload() {
			// 页面卸载时触发
			let that = this
			that.sendData({
				type: 'page_leave',
				path: that.path,
				params: ""
			});
		},
		methods: {
			// 连接
			connectWebSocket() {
				let that = this
				const wsUrl = "wss://gxtravel.ncscloudmedia.com/wss"; // 确保使用wss（WebSocket Secure）如果服务器支持
				that.ws = new WebSocket(wsUrl);
				that.ws.onopen = () => {
					//console.log("连接已建立");
					// 这里可以发送初始化消息等
					that.sendData({
						type: 'page_enter',
						path: that.path,
						params: ""
					});
				};
				that.ws.onmessage = (event) => {
					console.log("Message from server ", event.data);
					// 处理接收到的消息
				};
				that.ws.onerror = (error) => {
					console.error("连接失败: ", error);
				};
				that.ws.onclose = () => {
					console.log("连接失败，重新连接");
					// 可以在这里尝试重新连接
					that.connectWebSocket();
				};
			},
			// 发送
			sendData(data) {
				if (this.ws && this.ws.readyState === WebSocket.OPEN) {
					// this.ws.send(data); // 确保发送JSON格式的数据
					this.ws.send(JSON.stringify(data)); // 确保发送JSON格式的数据
				} else {
					console.error("WebSocket is not open");
				}
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					index
				} = that;
				let obj = {
					size: offset,
					page: page + 1,
					status: index + 1
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await feedbackMyList(obj);
					if (result.code == 1) {
						that.page = page + 1;
						if (result.data.total == 0) {
							that.empty = true;
							that.status = 'nomore';
							return;
						}
						that.empty = false;
						setTimeout(() => {
							that.status = 'loadmore';
						}, 500);
						that.list = list.concat(result.data.list);
						that.totalPage = Math.ceil(result.data.total / offset);
					}
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			click(item) {
				this.index = item.index
				this.page = 0
				this.list = []
				this.getlist()
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #F5F5F5;
	}

	.list {
		background: white;
		font-size: 24rpx;
		margin: 32rpx;
		border-radius: 8rpx;
	}

	.list .flex {
		color: #999999;
		padding: 14rpx 24rpx;
	}

	.title {
		padding: 23rpx 24rpx;
		border-bottom: dotted 1rpx #CCCCCC;
	}
</style>